import {
  getHotSingerList
} from "../../utils/http"
Page({
  pageNumber: 1,
  data: {},
  onLoad() {
    this.getSingerList()
  },
  // 获取歌手列表
  async getSingerList() {
    wx.showLoading({
      title: '',
      mask:true
    })
    let res = await getHotSingerList({
      pageNumber: this.pageNumber
    })
    wx.hideLoading()
    let hotSingerList = this.data.hotSingerList || []
    hotSingerList.push(...res.list)
    this.setData({
      hotSingerList,
      nowPageNumber: res.nowPageNumber,
      totalPage: res.totalPage,
      hasMore: res.totalPage > res.nowPageNumber
    })

  },
   // 加载更多
   loadMore() {
    if (this.data.hasMore) {
      this.pageNumber += 1
      this.getSingerList()
    }
  },
  toSingerSong(e) {
    let {
      id,
      singer
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/music/index?type=singer&id=${id}&navtitle=${singer}`,
    })
  },
  collection(e) {
    let {
      id
    } = e.currentTarget.dataset
    let music = this.data.hotSingerList.filter(item => item.singerId == id)[0]
    let collectionSinger = wx.getStorageSync('collectionSinger') || []
    // 循环收藏，找出是否收藏过
    let find_i = collectionSinger.findIndex(item => item.singerId == id)
    if (find_i !== -1) { // 同id的收藏过，删除原记录。后续会将数据添加到第一个
      collectionSinger.splice(find_i, 1)
    }
    collectionSinger.unshift(music)
    wx.setStorage({
      key: "collectionSinger",
      data: collectionSinger,
      success: () => {
        wx.showToast({
          title: '已加入收藏',
          icon: 'none'
        })
      }
    })
  },
})